package com.woniuxy.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @program: tsm
 * @description:
 * @create: 2024-10-22 16:48
 **/
@Service
public class AmapService {
    @Value("${amap.api.key}")
    private String apiKey;

    private final RestTemplate restTemplate;

    public AmapService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String searchPlace(String keyword) {
        String url = String.format("https://restapi.amap.com/v3/place/text?key=%s&keywords=%s", apiKey, keyword);
        return restTemplate.getForObject(url, String.class);
    }

    public byte[] getStaticMap(String location) {
        String url = String.format("https://restapi.amap.com/v3/staticmap?key=%s&location=%s&size=600*300&zoom=12", apiKey, location);
        ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
        return response.getBody();  // 返回字节数组
    }
}